﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class operate_operateTestQuestion : System.Web.UI.Page
{
    string[] keys;
    protected void Page_Load(object sender, EventArgs e)
    {
        keys = Request.QueryString.AllKeys;
        //规范：1.operate 2.table 3.id
        if (Request.QueryString[keys[0]].ToString().Trim() == "del")
        {
            //删除用户
            DelTestQuestion();

        }
        else if (Request.QueryString[keys[0]].ToString().Trim() == "add")
        {
            //添加用户
            
            AddTestQuestion();
        }
        else if (Request.QueryString[keys[0]].ToString().Trim() == "update")
        {
            //修改用户
            UpdateTestQuestion();

        }
        Response.End();
    }
    public void UpdateTestQuestion()
    {
        if (Request.QueryString[keys[1]].ToString().Trim() == "tb_test_questions")
        {

            List<string> set = new List<string>();
            for (int i = 3; i < keys.Length; i++)
            {
                set.Add(String.Format(" {0} = '{1}' ", keys[i], Request.QueryString[keys[i]]));
            }
            string s = string.Join(" , ", set);
            string sql = "update  tb_test_questions set " + s
                + " where test_question_id ='" + Request.QueryString[keys[2]] + "'";
                
            int k = SqlServer.ExecuteNonQuery(sql);
            if (k > 0)
            {
                Response.Write(k.ToString());
            }
            else
            {
                Response.Write("0");//删除失败
            }
        }
        #region
        if (Request.QueryString[keys[1]].ToString().Trim() == "tb_test_info")
        {
            List<string> set = new List<string>();
            for (int i = 4; i < keys.Length; i++)
            {
                set.Add(String.Format(" {0} = '{1}' ", keys[i], Request.QueryString[keys[i]]));
            }
            string s = string.Join(" , ", set);
            string sql = "update  tb_test_info set " + s
                + " where test_question_id ='" + Request.QueryString[keys[3]] + "' and course_id ='" + Request.QueryString[keys[2]] + "'";

            int k = SqlServer.ExecuteNonQuery(sql);
            if (k > 0)
            {
                Response.Write(k.ToString());
            }
            else
            {
                Response.Write("0");//删除失败
            }
        }
        #endregion

    }
    public void AddTestQuestion()
    {

        #region
        if (Request.QueryString[keys[1]].ToString().Trim() == "tb_test_questions")
        {            
            //该课程是否存在
            string sql1 = "select count(*) from tb_course where course_id ='" + Request.QueryString[keys[3]] + "'";
            string  a = SqlServer.ExecuteScalar(sql1);
            if (a != "1")
            {
                Response.Write("-2");
                return;
            }
            string sql = "INSERT INTO [dbo].[tb_test_questions]" +
                            "           ([test_question_name]" +
                            "           ,[test_score]" +
                            "           ,[test_difficulty_level]" +
                            "           ,[course_id]" +
                            "           ,[radio_questions_sum]" +
                            "           ,[mutil_questions_sum]" +
                            "           ,[judge_questions_sum]" +
                            "           ,[answer_questions_sum])" +
                            "     VALUES("+
                        String.Format("'{0}',", Request.QueryString[keys[2]]) +
                        String.Format("'{0}',", Request.QueryString[keys[8]]) +
                        String.Format("'{0}',", Request.QueryString[keys[9]]) +
                        String.Format("'{0}',", Request.QueryString[keys[3]]) +
                        String.Format("'{0}',", Request.QueryString[keys[4]])+
                        String.Format("'{0}',", Request.QueryString[keys[5]]) +
                        String.Format("'{0}',", Request.QueryString[keys[6]]) +
                        String.Format("'{0}')", Request.QueryString[keys[7]]);
            int k = SqlServer.ExecuteNonQuery(sql);
            if (k > 0)
            {
                Response.Write(k.ToString());
            }
            else
            {
                Response.Write("0");//删除失败
            }
        }
        #endregion

        #region
        if (Request.QueryString[keys[1]].ToString().Trim() == "tb_test_info")
        {

            string sql = "INSERT INTO [dbo].[tb_test_info]" +
                        "           ([course_id]" +
                        "           ,[test_question_id]" +
                        "           ,[test_time]" +
                        "           ,[test_location])" +
                        "     VALUES("+
                        String.Format("'{0}',", Request.QueryString[keys[2]]) +
                        String.Format("'{0}',", Request.QueryString[keys[3]]) +
                        String.Format("'{0}',", Request.QueryString[keys[4]]) +
                        String.Format("'{0}')", Request.QueryString[keys[5]]);
            int k = SqlServer.ExecuteNonQuery(sql);
            if (k > 0)
            {
                Response.Write(k.ToString());
            }
            else
            {
                Response.Write("0");//删除失败
            }
        }
        #endregion
    }
    public void DelTestQuestion()
    {
        if (Request.QueryString[keys[1]].ToString().Trim() == "tb_test_questions")
        {
            string test_question_id = Request.QueryString[keys[2]];
            string sql = "delete from tb_test_questions where test_question_id ='" + test_question_id + "'";
            int i = SqlServer.ExecuteNonQuery(sql);
            if (i > 0)
            {
                Response.Write(i.ToString());
            }
            else
            {
                Response.Write("0");//删除失败
            }
        }

        if (Request.QueryString[keys[1]].ToString().Trim() == "tb_test_info")
        {
            string test_question_id = Request.QueryString[keys[2]];
            string course_id = Request.QueryString[keys[3]];

            string sql = "delete from tb_test_info where test_question_id ='" + test_question_id + "' and course_id ='"+course_id +"'";
            int i = SqlServer.ExecuteNonQuery(sql);
            if (i > 0)
            {
                Response.Write(i.ToString());
            }
            else
            {
                Response.Write("0");//删除失败
            }
        }
    }
}